package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.incar.chuxing.R;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.InviteFriendsBean;
import com.incar.chuxing.utils.Constants;
import com.incar.chuxing.utils.DensityUtils;
import com.incar.chuxing.utils.ImageLoader;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.SharedPreferencesKey;
import com.incar.chuxing.utils.SharedPreferencesUtils;
import com.incar.chuxing.utils.StatusBarUtil;
import com.incar.chuxing.utils.Toast.ToastCompat;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class InviteFriendActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_name)
    TextView titleName;
    private Context mContext = this;
    private Button button_share_wechat;
    private Button button_share_wxcircle;
    private Button button_share_qq;
    private ImageView inviteIv;
    private RelativeLayout toolbar_invite;
    private int fromActivity;

    @Override
    public int getLayoutId() {
        return R.layout.activity_invite;
    }


    @Override
    public void initData() {
        super.initData();
        fromActivity = getIntent().getIntExtra(IntentKey.SHARE_TYPE, 2); //1从支付页面跳来  2个人中心跳来
        inviteFriends();
    }


    @Override
    public void initView() {
        titleName.setText("邀请好友");
        inviteIv = (ImageView) findViewById(R.id.invite_iv);
        button_share_wechat = (Button) findViewById(R.id.button_share_wechat);
        button_share_wxcircle = (Button) findViewById(R.id.button_share_wxcircle);
        button_share_qq = (Button) findViewById(R.id.button_share_qq);
        button_share_wechat.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                inviteFriendsShare(SHARE_MEDIA.WEIXIN);
            }
        });
        button_share_wxcircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                inviteFriendsShare(SHARE_MEDIA.WEIXIN_CIRCLE);
            }
        });
        button_share_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inviteFriendsShare(SHARE_MEDIA.QQ);
            }
        });
    }


    @OnClick({R.id.title_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                if (fromActivity == 1) {
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else if (fromActivity == 2) {
                    finish();
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (fromActivity == 1) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else if (fromActivity == 2) {
            finish();
        }
    }


    private void inviteFriends() {

        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).inviteFriends(((MyApplication) getApplication()).getRequestFieldMap(), "1").enqueue(new Callback<InviteFriendsBean>() {
            @Override
            public void onResponse(Call<InviteFriendsBean> call, Response<InviteFriendsBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        String ivUrl = ((MyApplication) getApplication()).getImageUrl() + response.body().getAttachment().getInvitePicture();
                        ImageLoader.getInstance().displayImage(getApplicationContext(), ivUrl, inviteIv);
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<InviteFriendsBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void inviteFriendsShare(final SHARE_MEDIA platform) {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).inviteFriends(((MyApplication) getApplication()).getRequestFieldMap(), "1").enqueue(new Callback<InviteFriendsBean>() {
            @Override
            public void onResponse(Call<InviteFriendsBean> call, Response<InviteFriendsBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        UMWeb web = new UMWeb(response.body().getAttachment().getUrl() + "?uid=" + response.body().getAttachment().getSessionUser().getUid());
                        web.setTitle(response.body().getAttachment().getTitle());
                        web.setDescription(response.body().getAttachment().getDescriptions());
                        Log.i("inviteContent", response.body().getAttachment().getTitle() + "     " + response.body().getAttachment().getDescriptions());
                        web.setThumb(new UMImage(mContext, ((MyApplication) getApplication()).getImageUrl() + response.body().getAttachment().getPicture()));
                        new ShareAction(InviteFriendActivity.this).setPlatform(platform)
                                .withMedia(web)
                                .setCallback(umShareListener)
                                .share();
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<InviteFriendsBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastCompat.makeText(InviteFriendActivity.this, "分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastCompat.makeText(InviteFriendActivity.this, "分享失败啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastCompat.makeText(InviteFriendActivity.this, "分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
}
